//
//  MRZoomScrollView.m
//  ScrollViewWithZoom
//
//  Created by xuym on 13-3-27.
//  Copyright (c) 2013年 xuym. All rights reserved.
//
#import "MRZoomScrollView.h"
#define MRScreenWidth      CGRectGetWidth([UIScreen mainScreen].applicationFrame)
#define MRScreenHeight     CGRectGetHeight([UIScreen mainScreen].applicationFrame)
#define IMWidth (Screen_Width-28)
#define StartY  150
@interface MRZoomScrollView (Utility)

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;

@end

@implementation MRZoomScrollView

@synthesize imageView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = self;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)initImageView
{
    imageView = [[UIImageView alloc]init];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    self.imHeight = (Screen_Width-28)/(self.imageData.size.width/self.imageData.size.height);
    
    imageView.frame = CGRectMake(0,150,(Screen_Width-28),self.imHeight);
    [imageView setImage:self.imageData];
    [self addSubview:imageView];
    
    self.aceDrawView = [[ACEDrawingView alloc] initWithFrame:CGRectMake(0,150,(Screen_Width-28),self.imHeight)];
    self.aceDrawView.forScreenFlag = YES;
    self.aceDrawView.doubleClickflag = YES;
    self.aceDrawView.backgroundColor = [UIColor clearColor];
    
    [self addSubview:self.aceDrawView];
    //self.aceDrawView.backgroundColor = [UIColor redColor];
//    
//    [self setShowsHorizontalScrollIndicator:NO];
//    [self setShowsVerticalScrollIndicator:NO];
    
   // [self setMaximumZoomScale:3.0];
    
//    float minimumScale = self.frame.size.width / self.aceDrawView.frame.size.width;
//    self.scale = minimumScale;
//    [self setMinimumZoomScale:0.3];
//    [self setZoomScale:0.3];
   // self.pinchGestureRecognizer.enabled = NO;
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [self addGestureRecognizer:pinch];
    
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view NS_AVAILABLE_IOS(3_2)
{
//[scrollView setZoomScale:0.5 animated:NO];
}
//- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
//{
//    if(self.panflag == Pan){
//    }
//    else{
//    self.scale = scale;
//        NSLog(@"%f",scale);
//        CGRect r = imageView.frame;
//        r.origin.y = 300 - scale*300;
//        if(r.origin.y <= 0){
//            r.origin.y = 0;
//        }
//        imageView.frame = r;
//    [scrollView setZoomScale:scale animated:NO];
//    }
//}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
    if(pinch.state == UIGestureRecognizerStateEnded){
        self.scale = 1.0;
        return;
    }
    if(self.panflag != Pan){
    CGFloat f = 1.0 - (self.scale - pinch.scale);
    CGAffineTransform currentTrans = self.aceDrawView.transform;
    CGAffineTransform newTrans = CGAffineTransformScale(currentTrans,f, f);
    [self.aceDrawView setTransform:newTrans];
    [self.imageView setTransform:newTrans];
    self.contentSize = CGSizeMake(self.imageView.frame.size.width,self.imageView.frame.size.height);
    CGRect r = self.aceDrawView.frame;
    if(self.imageView.frame.size.width > IMWidth){
        CGFloat offY;
        if(self.imageView.frame.size.height > self.frame.size.height)
            offY = (self.imageView.frame.size.height - self.frame.size.height)/2;
        else
            offY = 0;
   // self.contentOffset = CGPointMake((self.imageView.frame.size.width - IMWidth)/2,offY);
        r.origin.x = 0;
        r.origin.y = StartY - (self.imageView.frame.size.height/self.imHeight)*50;
        if(r.origin.y <= 0)
        r.origin.y = 0;
    }
    else{
        r.origin.x = (IMWidth - self.imageView.frame.size.width)/2;
        r.origin.y = StartY + (self.imHeight - self.imageView.frame.size.height)/2;
    }
    self.aceDrawView.frame = r;
    self.imageView.frame = r;
    self.scale = pinch.scale;
    }
    
//    NSLog(@"x = %f  y = %f  w = %f  h = %f",self.imageView.frame.origin.x,self.imageView.frame.origin.y,self.imageView.frame.size.width,self.imageView.frame.size.height);
//
//    NSLog(@"pinch-scale  %f",pinch.scale);
//    NSLog(@"f == %f",f);
}
#pragma mark - View cycle
- (void)dealloc
{
   // [super dealloc];
}

@end
